package com.ruoyi.binance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.binance.service.test.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.binance.domain.BinanceSimulateConf;
import com.ruoyi.binance.service.IBinanceSimulateConfService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模拟配置Controller
 * 
 * @author zq
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/binance/simulateConf")
public class BinanceSimulateConfController extends BaseController
{
    @Autowired
    private IBinanceSimulateConfService binanceSimulateConfService;
    @Autowired
    private AsyncAPriceMonitoringTest asyncAPriceMonitoringTest;
    @Autowired
    private AsyncAutoSingleCellTest asyncAutoSingleCellTest;
    @Autowired
    private AsyncAveBuySellTest asyncAveBuySellTest;
    @Autowired
    private AsyncAveragePriceTest asyncAveragePriceTest;
    @Autowired
    private AsyncAveBuySellPlusTest asyncAveBuySellPlusTest;
    @Autowired
    private AsyncAveragePricePlusTest asyncAveragePricePlusTest;
    @Autowired
    private Async15MACDTest async15MACDTest;

    /**
     * 查询模拟配置列表
     */
    @PreAuthorize("@ss.hasPermi('binance:simulateConf:list')")
    @GetMapping("/list")
    public TableDataInfo list(BinanceSimulateConf binanceSimulateConf)
    {
        startPage();
        List<BinanceSimulateConf> list = binanceSimulateConfService.selectBinanceSimulateConfList(binanceSimulateConf);
        return getDataTable(list);
    }

    /**
     * 导出模拟配置列表
     */
    @PreAuthorize("@ss.hasPermi('binance:simulateConf:export')")
    @Log(title = "模拟配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BinanceSimulateConf binanceSimulateConf)
    {
        List<BinanceSimulateConf> list = binanceSimulateConfService.selectBinanceSimulateConfList(binanceSimulateConf);
        ExcelUtil<BinanceSimulateConf> util = new ExcelUtil<BinanceSimulateConf>(BinanceSimulateConf.class);
        util.exportExcel(response, list, "模拟配置数据");
    }

    @GetMapping(value = "/test")
    public AjaxResult test(Long id)
    {
        BinanceSimulateConf bean = binanceSimulateConfService.selectBinanceSimulateConfById(id);
        bean.setRemark("回测中");
        int i = binanceSimulateConfService.updateBinanceSimulateConf(bean);
        if("price_monitoring".equals(bean.getMonitoringType())){
            asyncAveragePriceTest.getAveragePrice(bean.getId());
        }
        if ("a_price_monitoring".equals(bean.getMonitoringType())){
            asyncAPriceMonitoringTest.getAPriceMonitoring(bean.getId());
        }
        if("average_buy_sell".equals(bean.getMonitoringType())){
            asyncAveBuySellTest.getAPriceMonitoring(bean.getId());
        }
        if ("auto_single_cell".equals(bean.getMonitoringType())){
            asyncAutoSingleCellTest.getAPriceMonitoring(bean.getId());
        }
        if("average_buy_sell_plus".equals(bean.getMonitoringType())){
            asyncAveBuySellPlusTest.getAveragePrice(bean.getId());
        }
        if("price_monitoring_plus".equals(bean.getMonitoringType())){
            asyncAveragePricePlusTest.getAveragePrice(bean.getId());
        }
        if ("15_macd".equals(bean.getMonitoringType())){
            async15MACDTest.getAPriceMonitoring(bean.getId());
        }
        return success(i);
    }


    /**
     * 获取模拟配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('binance:simulateConf:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(binanceSimulateConfService.selectBinanceSimulateConfById(id));
    }

    /**
     * 新增模拟配置
     */
    @PreAuthorize("@ss.hasPermi('binance:simulateConf:add')")
    @Log(title = "模拟配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BinanceSimulateConf binanceSimulateConf)
    {
        return toAjax(binanceSimulateConfService.insertBinanceSimulateConf(binanceSimulateConf));
    }

    /**
     * 修改模拟配置
     */
    @PreAuthorize("@ss.hasPermi('binance:simulateConf:edit')")
    @Log(title = "模拟配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BinanceSimulateConf binanceSimulateConf)
    {
        return toAjax(binanceSimulateConfService.updateBinanceSimulateConf(binanceSimulateConf));
    }

    /**
     * 删除模拟配置
     */
    @PreAuthorize("@ss.hasPermi('binance:simulateConf:remove')")
    @Log(title = "模拟配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(binanceSimulateConfService.deleteBinanceSimulateConfByIds(ids));
    }
}
